package com.qm.console.component.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fangzhipeng on 2017/11/13
 * @author fangzhipeng
 * 双向mapping工具类
 */
public class BothMapping<left, right> {

    private Map<left, right> leftMap = new HashMap<>();
    private Map<right, left> rightMap = new HashMap<>();

    private BothMapping() {

    }

    public static <left, right> BothMapping<left, right> create() {
        return new BothMapping<left, right>();
    }


    public void put(left left, right right) {
        if (null != leftMap.put(left, right)) {
            throw new RuntimeException("left is already exists !!");
        }
        if (null != rightMap.put(right, left)) {
            throw new RuntimeException("right is already exists !!");
        }
    }

    public right getByLeft(left left) {
        return leftMap.get(left);
    }

    public left getByRight(right right) {
        return rightMap.get(right);
    }

}
